package com.summer.data.unuse;

import com.summer.data.type.Callback1;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 345480567@qq.com
 */
public class DataSourceAll {

    private static List<HikariDataSource> dataSourceList;

    public static List<HikariDataSource> dataSourceList() {
        if (dataSourceList == null) {
            dataSourceList = new ArrayList<>();

//            setupMysql();
//            setupOracle();

        }
        return dataSourceList;
    }

    public static void with(Callback1<DataSource> func) {
        dataSourceList().forEach(dataSource -> func.apply(dataSource));
    }

    public static void pick(String dbName, Callback1<DataSource> func) {
        dataSourceList()
                .stream()
                .filter(dataSource -> dataSource.getJdbcUrl().toLowerCase().indexOf(dbName.toLowerCase())>=0)
                .forEach(dataSource -> func.apply(dataSource));
    }

    private static void setupMysql() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/dataup?useSSL=false&useUnicode=true&characterEncoding=utf-8");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setMaximumPoolSize(1);
        dataSourceList.add(dataSource);
    }

    private static void setupOracle() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:oracle:thin:@//127.0.0.1:1523/ORCLPDB1");
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUsername("sd");
        dataSource.setPassword("123456");
        dataSource.setMaximumPoolSize(1);
        dataSourceList.add(dataSource);
    }
}
